1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.GwtCompatible;
20  import com.google.common.annotations.GwtIncompatible;
21  
22  import java.util.Collection;
23  import java.util.Comparator;
24  import java.util.NoSuchElementException;
25  import java.util.Set;
26  
27  import javax.annotation.Nullable;
28  
29  /**
30   * An empty immutable sorted set.
31   *
32   * @author Jared Levy
33   */
34  @GwtCompatible(serializable = true, emulated = true)
35  @SuppressWarnings("serial") // uses writeReplace(), not default serialization
36  class EmptyImmutableSortedSet<E> extends ImmutableSortedSet<E> {
37    EmptyImmutableSortedSet(Comparator<? super E> comparator) {
38      super(comparator);
39    }
40  
41    @Override
42    public int size() {
43      return 0;
44    }
45  
46    @Override public boolean isEmpty() {
47      return true;
48    }
49  
50    @Override public boolean contains(@Nullable Object target) {
51      return false;
52    }
53  
54    @Override public boolean containsAll(Collection<?> targets) {
55      return targets.isEmpty();
56    }
57  
58    @Override public UnmodifiableIterator<E> iterator() {
59      return Iterators.emptyIterator();
60    }
61  
62    @GwtIncompatible("NavigableSet")
63    @Override public UnmodifiableIterator<E> descendingIterator() {
64      return Iterators.emptyIterator();
65    }
66  
67    @Override boolean isPartialView() {
68      return false;
69    }
70  
71    @Override public ImmutableList<E> asList() {
72      return ImmutableList.of();
73    }
74  
75    @Override
76    int copyIntoArray(Object[] dst, int offset) {
77      return offset;
78    }
79  
80    @Override public boolean equals(@Nullable Object object) {
81      if (object instanceof Set) {
82        Set<?> that = (Set<?>) object;
83        return that.isEmpty();
84      }
85      return false;
86    }
87  
88    @Override public int hashCode() {
89      return 0;
90    }
91  
92    @Override public String toString() {
93      return "[]";
94    }
95  
96    @Override
97    public E first() {
98      throw new NoSuchElementException();
99    }
100 
101   @Override
102   public E last() {
103     throw new NoSuchElementException();
104   }
105 
106   @Override
107   ImmutableSortedSet<E> headSetImpl(E toElement, boolean inclusive) {
108     return this;
109   }
110 
111   @Override
112   ImmutableSortedSet<E> subSetImpl(
113       E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
114     return this;
115   }
116 
117   @Override
118   ImmutableSortedSet<E> tailSetImpl(E fromElement, boolean inclusive) {
119     return this;
120   }
121 
122   @Override int indexOf(@Nullable Object target) {
123     return -1;
124   }
125 
126   @Override
127   ImmutableSortedSet<E> createDescendingSet() {
128     return new EmptyImmutableSortedSet<E>(Ordering.from(comparator).reverse());
129   }
130 }